// 导入Koa包
const Koa = require('koa')
const commonsql = require('./db/common')

const cors = require('./lib/cors');
// const app = new Koa();

// 实例化app对象
// const app = new Koa()


// app.use(async (ctx, next)=>{
//   console.log('我是中间件1',ctx)
// //  const sqlData = await commonsql.getUserByName('test');
// //  console.log('sqlData', sqlData);
//   next()
// })


// 引入koa-router
const Router = require('koa-router')

const app = new Koa()
// 实例化koa-router
const router = new Router()
// 解决跨域问题
app.use(cors({credentials: false, allowMethods: ['GET', 'POST', 'OPTIONS']}));

// 编写路由函数
router.get('/getdata', async (ctx)=> {
  console.log('ctx', ctx.params)
  const {name,content} = ctx.query
  console.log('name', name,content);
  // console.log(new URL(ctx.request.url))
 const sqlData = await commonsql.insertComment(name,content);
//  console.log('sqlData', sqlData);
  ctx.body = {sucss:true, msg:'成功了'}

  // ctx.body = ctx
})

router.post('/info', (ctx)=> {
  ctx.body = ctx
})

// 注册路由
app.use(router.routes())


// 启动服务
app.listen(3000, ()=> {
  console.log('server is running at http://127.0.0.1:3000')
})

